package com.yulongtian.weekCompetition.month202212.week4;

import java.util.ArrayList;
import java.util.List;

/**
 * 第 325 场周赛
 * 可
 *
 * @author yulongTian
 * @create 2022-12-25 10:33
 */
public class Test11 {
    public static void main(String[] args) {
        String[] words = {"a", "b", "leetcode"};
        System.out.println(closetTarget(words, "leetcode", 0));
    }

    public static int closetTarget(String[] words, String target, int startIndex) {

        List<Integer> indexs = new ArrayList<>();
        int n = words.length;
        for (int i = 0; i < words.length; i++) {
            if (target.equals(words[i])) {
                indexs.add(i);
            }
        }

        int res = Integer.MAX_VALUE;

        for (Integer index : indexs) {
            res = Math.min(res, Math.abs(index - startIndex));
            res = Math.min(res, Math.abs(index + n - startIndex));
            res = Math.min(res, Math.abs(startIndex + n - index));
        }

        if (res == Integer.MAX_VALUE) {
            return -1;
        }


        return res;
    }
}
